<?php
class userhasroledao extends CI_Model{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	public function insert_role_to_user($user_id,$role_id){
		$data = array(
				'user_id' => $user_id,
				'role_id' => $role_id
		);
		$str = $this->db->insert_string('user_has_role', $data);
		$res = $this->db->query($str);
		if (!$res) {
			return false;
		} else {
			return $data;
		}
	}

	public function del($user_id, $role_id) {
		if (intval($role_id) > 0 && intval($user_id) > 0) {
			$this->db->where('role_id', $role_id);
			$this->db->where('user_id', $user_id);
			$this->db->delete('user_has_role');
			return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
		}
	}

	public function get_by_id($user_id, $role_id){
		$this->db->select('*');
		$this->db->from('user_has_role');
		$this->db->where('role_id',$role_id);
		$this->db->where('user_id',$user_id);
		$query = $this->db->get();
		$role = $query->row();
		return $role;
	}

	public function get_all_roles(){
		$this->db->select('*');
		$this->db->from('user_has_role');
		$query = $this->db->get();
		$role = $query->result();
		return $role;
	}

}
?>